package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.TUser;
import com.ruoyi.system.service.*;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.framework.web.service.SysPermissionService;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * 登录验证
 * 
 * @author ruoyi
 */
@RestController
public class SysLoginController
{
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private ISysPostService postService;

    @Resource
    private HttpSession session;

    @Resource
    private ITUserService itUserService;

    /**
     * 登录方法
     * 
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody)
    {
        session.setAttribute("URL","login");
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        System.out.println(token);
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 后台
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/admin")
    public AjaxResult admin(@RequestBody LoginBody loginBody)
    {
        session.setAttribute("URL","admin");
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        System.out.println(token);
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 获取用户信息
     * 
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo()
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();

        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        TUser tUser=itUserService.selectTuserByUserName(user.getUserName());
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        if(permissions.size()==0){
            System.out.println(permissionService.getMenuPermissionT(user).size()+"=========selectRolePermissionByTuserId");
            permissions = permissionService.getMenuPermissionT(user);
        }
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("tuser", tUser);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    /**
     * 获取路由信息
     * 
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters()
    {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus =null;
        System.out.println(userId);
        System.out.println(session.getAttribute("URL"));
        if(session.getAttribute("URL")==null){
            menus=menuService.selectMenuTreeByUserId(userId);
            System.out.println(menus.size()==0);
            if(menus.size()==0){
                menus=menuService.selectMenuTreeByTuserId(userId);
            }
        }else{
            if(session.getAttribute("URL").equals("login")){
                menus=menuService.selectMenuTreeByTuserId(userId);
                session.setAttribute("URL","login");
            }else if(session.getAttribute("URL").equals("admin")){
                menus=menuService.selectMenuTreeByUserId(userId);
                session.setAttribute("URL","admin");
            }
        }

        System.out.println(menus);
        if(userId==1){
            for(int i=0;i<menus.size();i++){
                if(menus.get(i).getMenuId()==2000){
                    menus.remove(i);
                }
                if(menus.get(i).getMenuId()==2026){
                    menus.remove(i);

                }
                if(menus.get(i).getMenuId()==2002){
                    menus.remove(i);

                }
                if(menus.get(i).getMenuId()==2007){
                    menus.remove(i);
                }
                if(menus.get(i).getMenuId()==2027){
                    menus.remove(i);
                }
                if(menus.get(i).getMenuId()==2034){
                    menus.remove(i);
                }
                for(int j=0;i<menus.size();i++){

                    if(menus.get(i).getMenuId()==2000){
                        menus.remove(i);
                    }
                    if(menus.get(i).getMenuId()==2026){
                        menus.remove(i);

                    }
                    if(menus.get(i).getMenuId()==2002){
                        menus.remove(i);

                    }
                    if(menus.get(i).getMenuId()==2007){
                        menus.remove(i);
                    }
                    if(menus.get(i).getMenuId()==2027){
                        menus.remove(i);
                    }
                    if(menus.get(i).getMenuId()==2034){
                        menus.remove(i);
                    }
                }
            }
        }


        return AjaxResult.success(menuService.buildMenus(menus));
    }
}
